package particle;

import processing.core.PVector;
import processing.core.*; 

public class Particle {
	  PVector loc;
	  PVector vel;
	  PVector acc;
	  float r;
	  float timer;
	  
	  // Another constructor (the one we are using here)
	  Particle(PVector l) {
	    acc = new PVector(0.005F,0.01F,0);
	    vel = new PVector( -0.01F, 0.5F, 0);
	    loc = l.get();
	    r = 20.0F;
	    timer = (float) ( (Math.random()) * 90 +60);
	  }

	  void run(PApplet pa) {
	    update();
	    render(pa);
	  }

	  // Method to update location
	  void update() {
		  float rand_vel = (float) ( (Math.random()-0.5) * 0.5);
		  float rand_vel2 = (float) ( (Math.random()-0.5) * 0.5);
		  vel.add( new PVector( rand_vel, rand_vel2, 0) );
		  loc.add(vel);
		  timer -= 1.0;
	  }

	  // Method to display
	  void render(PApplet pa) {
		pa.ellipseMode(pa.CENTER);
		pa.noStroke();
		
		pa.fill( 15,255);		
		pa.ellipse(loc.x,loc.y,r*timer/150,r*timer/150);
		//createGradient(pa, loc.x, loc.y, r*timer/150, pa.color(0, 0, 0), pa.color(255, 255, 255));
		
		pa.fill( 100 ,50);
		//pa.ellipse(loc.x-5,loc.y+5,r*timer/150,r*timer/150);
		
	    //displayVector(vel,loc.x,loc.y,10, pa);
	  }
	  
	  // Is the particle still useful?
	  boolean dead() {
	    if (timer <= 0.0) {
	      return true;
	    } else {
	      return false;
	    }
	  }
	  
	   void displayVector(PVector v, float x, float y, float scayl, PApplet pa) {
		pa.pushMatrix();
	    float arrowsize = 4;
	    // Translate to location to render vector
	    pa.translate(x,y);
	    pa.stroke(255);
	    // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
	    pa.rotate(v.heading2D());
	    // Calculate length of vector & scale it to be bigger or smaller if necessary
	    float len = v.mag()*scayl;
	    // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
	    pa.line(0,0,len,0);
	    pa.line(len,0,len-arrowsize,+arrowsize/2);
	    pa.line(len,0,len-arrowsize,-arrowsize/2);
	    pa.popMatrix();
	  } 
	   
	   void createGradient (PApplet pa, float x, float y, float radius, int c1, int c2){
			  float px = 0, py = 0, angle = 0;

			  // calculate differences between color components 
			  float deltaR = pa.red(c2)-pa.red(c1);
			  float deltaG = pa.green(c2)-pa.green(c1);
			  float deltaB = pa.blue(c2)-pa.blue(c1);
			  // hack to ensure there are no holes in gradient
			  // needs to be increased, as radius increases
			  float gapFiller = 8.0F;

			  for (int i=0; i< radius; i++){
			    for (float j=0; j<360; j+=1.0/gapFiller){
			      px = x+pa.cos(pa.radians(angle))*i;
			      py = y+pa.sin(pa.radians(angle))*i;
			      angle+=1.0/gapFiller;
			      int c = pa.color(
			      (pa.red(c1)+(i)*(deltaR/radius)),
			      (pa.green(c1)+(i)*(deltaG/radius)),
			      (pa.blue(c1)+(i)*(deltaB/radius)) 
			        );
			      pa.set( (int)(px), (int)(py), c);      
			    }
			  }
			  // adds smooth edge 
			  // hack anti-aliasing
			  pa.noFill();
			  pa.strokeWeight(3);
			  pa.ellipse(x, y, radius*2, radius*2);
			}

	}